package openglsuperbible.Chapter6;

import openglsuperbible.glutils.GLBatch;
import openglsuperbible.glutils.Shader;
import openglsuperbible.glutils.SimpleGLBatch;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*; 

/**
 *
 * @author Matthew
 */
public class Triangle {
     public static final int DISPLAY_HEIGHT = 600;
        public static final int DISPLAY_WIDTH = 800;
        
        private GLBatch triangleBatch;
        private Shader shaderIdentity;  
                             
        public void resizeGL() {   
            glViewport(0,0,DISPLAY_WIDTH ,DISPLAY_HEIGHT); 
        } 

        /**
         * Initialize anything need for the rendering context
         */
        public void initGL() {   
            glClearColor(0.0f,0.0f,1.0f,1.0f);     // blue background
            
            shaderIdentity = new Shader("Identity","Identity");
            
            triangleBatch = new SimpleGLBatch(GL_TRIANGLES,
                    new float[]{ 0.0f,  0.5f, 0.0f, 1.0f,    // vertex array  x,y,z,w  
                                -0.5f, -0.5f, 0.0f, 1.0f,            
                                 0.5f, -0.5f, 0.0f, 1.0f},      
                    new short[]{0, 1, 2}); // the index of the vertex         
        }   
            
        
        float vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
 
        /**
         * Clear the buffers.
         * Set the shader uniforms.
         * Draw the batch
         * Flip the buffers.
         */
        public void render() {
            // clear the window with current clearing color
            glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
            shaderIdentity.use(); 
            shaderIdentity.setUniform4("vColor", vRed); 
            triangleBatch.draw(shaderIdentity.getAttributeLocations());
            Display.update();  
        }   
            

        /**
         * Creates a window with an OpenGL context.
         */   
        public void create()  { 
            try{
            //Display    
            Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH,DISPLAY_HEIGHT)); 
            Display.setFullscreen(false);
            Display.setTitle("First Triangle");
            Display.create();            
                               
            //Keyboard         
            Keyboard.create();   
            //Mouse          
            Mouse.setGrabbed(false);
            Mouse.create(); 
            
            } catch (LWJGLException e) {
                
		System.exit(0);
	    }
            
            
            //OpenGL      
            initGL();    
            resizeGL();  
        }  
        
        /**
         * The game loop
         */
        public void run() 
        {
            while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {              
        
                render();

                Display.sync(60);
                Display.setVSyncEnabled(true);
            }

        }
        
        /**
         * Clean up any used resources.
         */
        public void destroy() {  
            //Methods already check if created before destroying.  
            Mouse.destroy();   
            Keyboard.destroy();
            Display.destroy();     
        } 
        
        /**
         * The main method for the program.
         * @param args 
         */
       public static void main(String[] args) {
            Triangle main = new Triangle(); 
            main.create(); 
            main.run();

            if(main != null) {  
                main.destroy();
            }                
        }  
            
}
